//
//  OrderCommitCell.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/2.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "OrderCommitCell.h"
#import "UIView+CHView.h"
@implementation OrderCommitCell{
    NSInteger number;
}

-(instancetype)initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
       self.iconImageview = [[UIImageView alloc] init];
        self.iconImageview.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:self.iconImageview];
        [_iconImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
            make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(10));
            make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
            make.height.mas_equalTo(scaleTopAndBottom(140));
        }];
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.text = @"商品名称";
        [self.contentView addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconImageview.mas_bottom).offset(scaleTopAndBottom(5));
            make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(15));
            make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-5));
            make.height.mas_equalTo(scaleTopAndBottom(20));
        }];
        self.storeLabel = [[UILabel alloc] init];
        self.storeLabel.text = @"库存量:20000";
        [self.contentView addSubview:self.storeLabel];
        [self.storeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nameLabel.mas_bottom).offset(scaleTopAndBottom(5));
            make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(15));
            make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-5));
            make.height.mas_equalTo(scaleTopAndBottom(10));
        }];
        self.priceLabel = [[UILabel alloc] init];
        self.priceLabel.text = @"￥500";
        [self.contentView addSubview:self.priceLabel];
        [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeLabel.mas_bottom).offset(scaleTopAndBottom(10));
            make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(15));
            make.height.mas_equalTo(scaleTopAndBottom(20));
        }];
        _bgview = [[UIView alloc] init];
        _bgview.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_bgview];
        [_bgview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeLabel.mas_bottom).offset(scaleTopAndBottom(10));
            make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-2));
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.width.equalTo(_priceLabel);
        }];
        self.numberLabel = [[UILabel alloc] init];
        self.numberLabel.textAlignment = NSTextAlignmentCenter;
        self.numberLabel.backgroundColor = [UIColor redColor];
        [_bgview addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_bgview.mas_centerX);
            make.top.and.bottom.equalTo(_bgview);
            make.width.equalTo(_bgview.mas_width).multipliedBy(0.4);
        }];
        self.plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.plusBtn setTitle:@"+" forState:UIControlStateNormal];
        [self.plusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.plusBtn addTarget:self action:@selector(plusTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_bgview addSubview:self.plusBtn];
        [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.numberLabel.mas_right);
            make.top.equalTo(_bgview.mas_top);
            make.bottom.equalTo(_bgview.mas_bottom);
            make.right.equalTo(_bgview.mas_right);
        }];
        self.minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.minusBtn setTitle:@"-" forState:UIControlStateNormal];
        [self.minusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.minusBtn addTarget:self action:@selector(minusTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_bgview addSubview:self.minusBtn];
        [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.numberLabel.mas_left);
            make.top.equalTo(_bgview.mas_top);
            make.bottom.equalTo(_bgview.mas_bottom);
            make.left.equalTo(_bgview.mas_left);
//            make.width.mas_equalTo()
            make.width.mas_equalTo(self.plusBtn);
        }];
        self.numberLabel.layer.borderWidth = 1;
        self.numberLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.minusBtn.layer.borderWidth = 1;
        self.minusBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.plusBtn.layer.borderWidth = 1;
        self.plusBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.plusBtn.backgroundColor = [UIColor greenColor];
        self.minusBtn.backgroundColor = [UIColor greenColor];
    }
    return self;
}

- (void)plusTapped:(UIButton *)btn {
    if (number <= 9999) {
        number++;
        [self updateNumberLabel];
       CGPoint btnPoint = [self convertPoint:self.bgview.center toView:self.superview];
//        CGPoint btnPoint = [self convertPoint:self.plusBtn.center fromView:self.superview];
//        CGPoint btn = [self conver]
        self.plusTapHandle(btnPoint);
    }
    
}

- (void)minusTapped:(UIButton *)btn {
    if (number) {
        number--;
        [self updateNumberLabel];
        self.minusTapHandle();
    }
    
}

- (void)updateNumberLabel {
    self.numberLabel.text = [NSString stringWithFormat:@"%zd", number];
}

//
//- (void)updateConstraints {
//
//    [self.plusBtn addCornerRadiusWithcorners:UIRectCornerTopRight | UIRectCornerBottomRight AndRadii:CGSizeMake(5, 5)];
//    [self.minusBtn addCornerRadiusWithcorners:UIRectCornerTopLeft | UIRectCornerBottomLeft AndRadii:CGSizeMake(5, 5)];
//    [super updateConstraints];
//}

@end
